<?php
namespace app\common\model;

class MemberCoin extends Base
{
	static $change=[
			'dec' => '减少',
			'inc' => '增加',
		];

	protected $readonly=['id','uid'];

	public static function change($key=null){
		if(!is_null($key)){
			return self::$change[$key];
		}else{
			return self::$change;
		}
	}

	public function getAccountByUid($uid,$coin_id=null){
		$where = ['uid'=>$uid];

		if(!is_null($coin_id)){
			$where['coin_id'] = $coin_id;
			$data = $this->where($where)->find();
			if(!$data){
				$this->error = '数据不存在';
				return false;
			}
			$data = $data->toArray();
			$data = $this->setField($data);
		}else{
			$datas = $this->where($where)->select();
			if(!$datas){
				$this->error = '数据不存在';
				return false;
			}
			$data = [];
			foreach($datas as $value):
				$value = $value->toArray();
				$data[] = $this->setField($value);
			endforeach;
		}
		return $data;
	}



	/**
	 * 判断用户金币是否充足
	 */
	public function isHigh($data=[]){
		if($this->getOneValue('id',$data)){
			return false;
		}else{
			$row = self::setField($data);
			$title = $row['coin_name'].'货币不足';
			return ['data'=>$title];
		}
	}

	public function getOneValue($field,$where){
    	return $this->where($where)->value($field);
    }

    private function setField($row){
		if(isset($row['coin_id'])){
			$row['coin_name'] = Coin::coinData($row['coin_id'])['coin_name'];
		}
		return $row;
	}

}
